package com.mals.pingpong;

import android.os.Bundle;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.view.View;
import android.view.Window;


public class MainActivity extends Activity {

	/**
	 * Provides access to the sensors supported by a specific device.
	 */
	private SensorManager sensorManager = null;
	
	/**
	 * View asociated to this Activity
	 */
	private View view = null;

	/**
	 * This method performs the initialization logic.
	 * It is common to pull all services and make association with the
	 * View in this part of the code.
	 * @param savedInstanceState
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		// the View that is going to be associated to this activity is here
		view = new PingPongView(this);
		
		// We need to use the ORIENTATION sensor, so we pull the service
		// and we keep a reference to it.
		sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
		
		// Associate View with the activity
		setContentView(view);
	}
	
	/**
	 * We override the onResume method to register the sensor listener
	 * everytime the application is resumed.
	 */
	@Override
	protected void onResume() {
		super.onResume();

		// register sensor
		sensorManager.registerListener((SensorEventListener)view,
				sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
				SensorManager.SENSOR_DELAY_GAME);
		
		// register sensor
		sensorManager.registerListener((SensorEventListener)view,
				sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
				SensorManager.SENSOR_DELAY_GAME);
	}

	/**
	 * When application is stopped we remove the registration of the
	 * sensor listener.
	 */
	@Override
	protected void onStop() {
		// Unregister the listener
		sensorManager.unregisterListener((SensorEventListener)view);
		super.onStop();
	}}
